/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.user;

import java.io.Serializable;
import java.util.Calendar;
import java.util.*;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.HttpServletRequest;
import model.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.hibernate.validator.internal.util.logging.Messages;
/**
 *
 * @author Administrator
 */
@ManagedBean(name = "apmb")
@SessionScoped
public class ApplicantManagedBean implements Serializable {
    @EJB
    private CvFacade cvFacade;
    @EJB
    private ApplicantFacade applicantFacade;
    private Applicant a= new Applicant();
    private Vacancy vac = new Vacancy();
    private Cv cv= new Cv();
    
    Date date= new Date();


    
    
    public Vacancy getVac() {
        return vac;
    }

    public void setVac(Vacancy vac) {
        this.vac = vac;
    }
   
 
    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
    public CvFacade getCvFacade() {
        return cvFacade;
    }

    public Cv getCv() {
        return cv;
    }

    public void setCv(Cv cv) {
        this.cv = cv;
    }

    public Applicant getA() {
        return a;
    }

    public void setA(Applicant a) {
        this.a = a;
    }



    public void setCvFacade(CvFacade cvFacade) {
        this.cvFacade = cvFacade;
    }

   
    

    public ApplicantFacade getApplicantFacade() {
        return applicantFacade;
    }

    public void setApplicantFacade(ApplicantFacade applicantFacade) {
        this.applicantFacade = applicantFacade;
    }

    

 
    
    private List<Vacancy> listVacancies;

    public void setListVacancies(List<Vacancy> listVacancies) {
        this.listVacancies = listVacancies;
    }

   

    /**
     * Creates a new instance of ApplicantManagedBean
     */
    public ApplicantManagedBean() {
    } 
    public String addapplicantandcv(Vacancy vac){
       String page ="thanks";
       try{
           String applicant = generateID();
           
           this.a.setApplicantID(applicant);
           this.a.setCreatedDate(new Date());
           this.a.setStatus(1);
           this.applicantFacade.create(this.a);
           
           String cvid = generateCVID();
           this.cv.setCvid(cvid);
           this.cv.setApplicantID(this.a);
           this.cv.setVacancyID(vac);
           this.cvFacade.create(cv);
           page = "index";
           
           
       }
        catch(Exception ex){
            System.out.println(ex.getMessage());
        }
       return page;
       
    }
    public List<Vacancy> getListVacancies() {
        return listVacancies;
    }
    private String generateID() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        
        return "A" + sdf.format(new Date());
    }
    private String generateCVID() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        
        return "cv" + sdf.format(new Date());
    }
}
